<template>
	<div style="padding:10px">
		<Form ref="loginForm" :model="form" :rules="rules">
			<FormItem label="类型" prop="document_receive_writing" v-if="type=='add'">
				<RadioGroup v-model="form.document_receive_writing">
					<Radio :label="item.dictionary_id" v-for="item in civil_arr" :key="item.dictionary_id">{{item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn}}</Radio>
				</RadioGroup>
			</FormItem>
			<FormItem :label="(form.document_receive_writing==613?'来':'发')+'文机关'" prop="civil_service.zh">
				<Input v-model="form.civil_service.zh"  style="width:400px"></Input>
			</FormItem>
			<FormItem :label="(form.document_receive_writing==613?'来':'发')+'文字号'" prop="letter_number.zh">
				<Input v-model="form.letter_number.zh"  style="width:400px"></Input>
			</FormItem>
			<FormItem label="收文时间" prop="receipt_time">
				<DatePicker v-model="form.receipt_time" type="datetime" format="yyyy-MM-dd HH:mm:ss" style="width: 200px"></DatePicker>
			</FormItem>
			<FormItem label="登记文号" prop="document_code">
				<Input v-model="form.document_code"  style="width:400px"></Input>
			</FormItem>
			<FormItem label="拟办意见" prop="options.zh" v-if="type=='add'">
				<Input v-model="form.options.zh" style="width:400px" type="textarea"></Input>
			</FormItem>
			<FormItem label="等级" prop="grade.zh">
				<Input v-model="form.grade.zh"  style="width:400px"></Input>
			</FormItem>
			<FormItem label="公文标题" prop="title.zh">
				<Input v-model="form.title.zh"  style="width:400px"></Input>
			</FormItem>
			<div style="margin-bottom:23px">
				<p style="margin-bottom:10px">公文内容：</p>
				<orh-editor v-model="form.content.zh" model="documents" :height="300"></orh-editor>
			</div>
			<FormItem label="附件" prop="file" v-if="type=='add'">
                <Upload :action="upload_urls" name="file" :headers="headers" :on-success="upSuccess" :on-error="upError">
                    <Button type="ghost" icon="ios-cloud-upload-outline">上传附件</Button>
                </Upload>
            </FormItem>
			<FormItem label="公文类型" prop="type">
				<Select v-model="form.type" style="width:200px;">
					<Option v-for="item in docutypes" :value="item.dictionary_id" :key="item.dictionary_id">{{ item.dictionary_name.zh?item.dictionary_name.zh:item.dictionary_name.mn }}</Option>
				</Select>
			</FormItem>
			<FormItem label="选择审核人" prop="examine_user" v-if="type=='add'">
				<Input v-model="exname" @on-focus="modal=true" placeholder="请选择审核人" style="width: 200px"></Input>
			</FormItem>
			<Button type="primary" @click="handleSubmit" long :loading="loading">{{type=='add'?'添加':'编辑'}}</Button>
		</Form>
		<Modal v-model="modal" title="选择审核人" @on-cancel="form.examine_user=''" width="350">
			<auditor @on-select="(id)=> {this.form.examine_user=id}"></auditor>
		</Modal>
		<!-- <orh-madel v-model="modal2" title="选择老师" @on-ok="chucktc" people="teacher"></orh-madel> -->
		<!-- <orh-madel v-model="modal3" title="选择学生" @on-ok="chucktc2" people="student"></orh-madel> -->
	</div>
</template>

<script>

export default {
    data() {
        return {
            addUrl: 'api/document/v1/document/edit',
            editUrl: 'api/document/v1/document/update/'+this.id,
			getypeurl: 'api/dictionary/v1/dictionary/pidquerys?document_type',
			civil_arr: [],
			partype: 'dep',
			duties: [],
			dutie_id: '',
            form:{
                title:{
                    zh:'',
                    en:'',
                    mn:'',
                },
                content: {
                    zh:'',
                    en:'',
                    mn:'',
				},
				letter_number:{
                    zh:'',
                    en:'',
                    mn:'',
				},
				civil_service:{
                    zh:'',
                    en:'',
                    mn:'',
				},
				grade:{
                    zh:'',
                    en:'',
                    mn:'',
				},
				document_code:'',
				receipt_time:'',
               	type: '',
				examine_user: '',
				options: {
					zh: '',
                    en: '',
                    mn: ''
				},
				file: [],
				document_receive_writing: ''
			},
			loading: false,
			modal: false,
			modal2: false,
			modal3: false,
			teachers: '',
			students: '',
			seartxt: '',
			rules: {
				'document_receive_writing': [
					{ required: true, message: '公文类型不能为空', trigger: 'change', type: 'number' }
				],
				'title.zh': [
					{ required: true, message: '公文标题不能为空', trigger: 'blur' }
				],
				'options.zh': [
					{ required: true, message: '拟办意见不能为空', trigger: 'blur' }
				],
				'letter_number.zh': [
					{ required: true, message: '字号不能为空', trigger: 'blur' }
				],
				'civil_service.zh': [
					{ required: true, message: '机关不能为空', trigger: 'blur' }
				],
				'grade.zh': [
					{ required: true, message: '等级不能为空', trigger: 'blur' }
				],
				'document_code': [
					{ required: true, message: '登记文号不能为空', trigger: 'blur' }
				],
				'type': [
					{ required: true, message: '公文类型不能为空', trigger: 'change', type: 'number'}
				],
				'examine_user': [
					{ required: true, message: '审核人不能为空', trigger: 'change', type: 'number'}
				]
			},
			examiners: [],
			examiners_copy: [],
        	docutypes:[],
			// 上传路径
			upload_urls: this.$project.axios.baseUrl +'api/user/v1/upload',
            headers: {
                'Authorization': 'Bearer '+(localStorage[ this.$project.axios.Authorization] || '')
            }
        }
    },
    props: {
        id: Number,
        type: String,
        editdt: Object
    },
	components: {
		Auditor: re=> require(['&/Public/SelectPeople/auditor.vue'],re)
	},
    methods: {
		panHide() {
            this.$emit('close')
            this.$emit('reload')
		},
		datecg(date) {
		//	this.form.receipt_time = date
		},
		// 点击添加按钮将数据发送给后端
		handleSubmit() {
			this.$refs.loginForm.validate((valid) => {
				if (valid) {
					if(this.form.receipt_time=='') {
						this.$Message.error('请选择收文时间');
						return false
					}
					if(this.type=='add') {
                        this.create()
                    } else {
                        this.update()
                    }
				} else {
					this.$Message.error('验证失败！');
				}
			})
        },
        create() {
			this.loading = true
			this.form.receipt_time= this.transDate(this.form.receipt_time)
            this.$http.post(this.addUrl,this.form).then(val=> {
                if(val.code==200) {
                    this.panHide();
                    this.loading = false
                } else {
                    this.loading = false
                }
            })
        },
        update() {
			 this.loading = true
			this.form.receipt_time= this.transDate(this.form.receipt_time)
            this.$http.put(this.editUrl,this.form).then(val=>{
                this.loading = false
                this.$Message.success('修改成功！')
                this.panHide();
            }).catch(err=> {
                this.loading = false
                console.log(err)
            })
        },
		ptchange(type) {
			if(type=='dep') {
				this.examiners = this.examiners_copy
			} else {
				this.examiners = []
				this.dutie_id = ''
			}
		},
		chax(id) {
            let url = `api/user/v1/get/teacher/user/list?duties_id=[${id}]`
			this.$http.get(url).then(res=>{
				this.examiners = res.data
			}).catch(
				err => this.$Message.error('人员获取失败！')
			)
		},
		zwload() {
			this.$http.get('api/user/v1/get/duties/list').then(res=> {
				this.duties = res.data.map(item=> ({
                    value: item.id,
                    title: item.duties_name
                }))
			}).catch(
				err => this.$Message.error('职务加载失败！')
			)
        },
		loadStudent(id) {
			this.$http.get('api/student/v1/studentlearn/studentname?eclass_id='+id).then(res=> {
                let dt = res.data.map((item)=> ({
                    user_id: item.user_id,
                    student_name: item.studentbase.student_name
                }))
                this.studentlist = dt
			}).catch(
				err => console.log(err)
			)
        },
		// 搜索人员
		toSearch(txt) {
			if(txt.length!=0) {
				this.examiners = this.examiners_copy.filter(item=> {
					return new RegExp(txt).test(item.teacher_name)
				})
			} else {
				this.examiners = this.examiners_copy
			}
		},
		// 获取年级信息
		getAllGrades() {
			this.$http.get('api/base/v1/get/grade/list').then(
				res=> {
					this.gradeslist = this.process(res.data,'grade_name')
				}
			).catch(
				err=> console.log(err)
			)
		},
		process(data,key) {
			let arr = []
			data.forEach(item => {
				item.title = item.label = item[key]
				item.value = item.id
				if(item.childs) {
					item.loading = false,
					item.children = []
				}
				arr.push(item)
			})
			return arr
		},
		// 获取年级下班级信息
        getClasses(value) {
            let id = value.slice(-1)
            this.$http.get('api/eclass/v1/eclass/eclasslist/'+id).then(
				res=> {
					this.classlist = this.process(res.data,'eclass_name')
				}
			).catch(
				err=> console.log(err)
			)
        },
		getType(){
			this.$http.get(this.getypeurl).then(val=>{
				this.docutypes = val.data.document_type
			}).catch(
				err => console.log(err)
			)
		},
		getExamine(){
			// api/user/v1/user/parent/survey/user
			this.$http.get('api/user/v1/get/teacher/user/list').then(val=>{
				this.examiners = val.data
				this.examiners_copy = val.data
			}).catch(
				err => console.log(err)
			)
		},
		gettpty() {
			this.$http.get('api/dictionary/v1/dictionary/pidquerys?document_receive_writing').then(val=>{
				this.civil_arr = val.data.document_receive_writing
			}).catch(
				err => console.log(err)
			)
		},
		upSuccess(res, file) {
            // console.log(res.data.id)
            this.form.file.push(res.data.id)
        },
        upError(res, file) {
            // console.log(res, file)
		},
		chucktc(data) {
			this.teachers = JSON.stringify(data.map(it=>it.title))
			this.form.teacher = data.map(it=>it.id)
		},
		chucktc2(data) {
			this.students = JSON.stringify(data.map(it=>it.title))
			this.form.student = data.map(it=>it.id)
        },
        // 获取单条数据
        getDetail() {
            this.$http.get('api/document/v1/document/getone/'+this.id).then(val=> {
                let datas = val.data
                
                this.form = datas
            }).catch(err=> {
                console.log(err)
            })
        }
    },
	computed: {
		exname() {
			let bool = this.examiners.find(item=>this.form.examine_user==item.user_id)
			if(bool) {
				return bool.teacher_name.zh
			} else {
				return ''
			}
		}
	},
    created(){
    	this.getAllGrades()
		this.getType()
		this.gettpty()
		this.getExamine()
        this.zwload()
        if(this.type=='edit') {
            this.getDetail()
        }
	}
}
</script>


<style scoped>

</style>